<template>
	<view class="executeResult">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="维保管理"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			@clickRight="arrow"
			right-text="申请"
			right-icon="plus"
		></uni-nav-bar>
		<view class="executeResult_top">
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="executeResult_main">
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="executeResult_floot">
			<uni-popup ref="popup" type="dialog">
				<view class="popContent">
					<view class="main">
						<dil-Form :formId="706" v-model="formValue" labelPosition="left" ref="form"></dil-Form>
						<view class="buttonClass">
							<button type="warn" @click="cancelExcuse" size="mini">取消</button>
							<button type="primary" class="button" @click="onclickConfirm" size="mini">上报</button>
						</view>
					</view>
				</view>
			</uni-popup>
		</view>
		<view class="executeResult_floot">
			<uni-popup ref="popup1" type="dialog">
				<view class="popContent">
					<view class="main">
						<dil-Form :formId="730" v-model="formValue1" labelPosition="left" ref="form"></dil-Form>
						<view class="buttonClass">
							<button type="warn" @click="cancelExcuse1" size="mini">取消</button>
							<button type="primary" class="button" @click="confirmApprove(formValue1)" size="mini">确认</button>
						</view>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			formValue: {
				personnelId: null
			},
			formValue1: {},
			options: {
				requestUrl: '/api/v1/tms/getcomprehensiveresults?apiId=684',
				requestQuery: {},
				cardSlotList: [
					{
						label: '驳回',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'refuse',
						arguments: {
							approveKey: 'alternateFields10',
							messageTitle: '是否驳回'
						},
						showCondition: '(item.alternateFields6 == null || item.alternateFields7 == null ) && item.alternateFields10 == null'
					},
					{
						label: '班组长审批',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'excuteMakeTeam',
						arguments: {
							approveKey: 'alternateFields6',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields6 == null && item.alternateFields10 == null'
					},
					{
						label: '维修协调员审批',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'excuteMakeCoordinate',
						arguments: {
							approveKey: 'alternateFields17',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields6 != null && item.alternateFields7 == null'
					},
					{
						label: '维保确认',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'excuteMakeManager',
						arguments: {
							approveKey: 'alternateFields7',
							messageTitle: '是否审批'
						},
						showCondition: 'item.alternateFields17 != null && item.alternateFields7 == null'
					},
					{
						label: '复岗申请',
						icon: 'wlIcon-rizhiyichang',
						emitEvent: 'workAppoint',
						arguments: {
							approveKey: 'alternateFields8',
							messageTitle: '是否申请复岗'
						},
						showCondition: 'item.alternateFields7 != null && item.alternateFields8 == null'
					}
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			}
		};
	},
	onShow() {
		uni.$emit('onShow', 1);
		this.getExecute();
	},
	mounted() {
		this.getExecuteDebounce = this.Tool.debounce((data) => {
			this.getExecute(data);
		}, 1500);
	},
	methods: {
		onSearch(item) {
			this.getExecuteDebounce(item);
		},
		back() {
			// #ifdef APP-PLUS
			uni.navigateBack();
			// #endif

			// #ifdef H5
			this.$router.back();
			// #endif
		},
		arrow() {
			this.formValue.personnelId = uni.getStorageSync('personnelId');
			this.$refs.popup.open();
		},
		cancelExcuse() {
			this.formValue = {};
			this.$refs.popup.close();
		},
		cancelExcuse1() {
			this.formValue1 = {};
			this.$refs.popup1.close();
		},
		emitEvent(eventName, cardItem) {
			if (eventName == 'excuteMakeManager') {
				cardItem[cardItem.approveKey] = uni.getStorageSync('personnelName');
				this[eventName](cardItem);
			} else {
				cardItem[cardItem.approveKey] = uni.getStorageSync('personnelName');
				let that = this;
				uni.showModal({
					cancelText: '取消',
					confirmText: '确认',
					title: '提示',
					content: cardItem.messageTitle,
					success: (res) => {
						if (res.confirm) {
							that.confirmApprove(cardItem);
						} else if (res.cancel) {
							uni.showToast({
								title: '用户取消操作！',
								icon: 'none',
								duration: 3000
							});
						}
					}
				});
			}
		},
		//获取执行实绩
		async getExecute(data = {}) {
			data.resultType = '维保';
			data.personnelName = data.personnelName || uni.getStorageSync('personnelName');
			data.personnelPost = data.personnelPost || uni.getStorageSync('personnelPost');
			data.personnelTeam = data.personnelTeam || uni.getStorageSync('personnelTeam');
			this.options.requestQuery = { ...data };
		},
		onclickConfirm() {
			let that = this;
			this.$refs['form'].validateForm((data) => {
				if (data.flag) {
					uni.showModal({
						cancelText: '取消',
						confirmText: '确认',
						title: '提示',
						content: '确定是否维保',
						success: (res) => {
							if (res.confirm) {
								that.confirmExecute();
							} else if (res.cancel) {
								uni.showToast({
									title: '用户取消操作！',
									icon: 'none',
									duration: 3000
								});
							}
						}
					});
				}
			});
		},
		async confirmExecute() {
			let data = { ...this.formValue };
			data.resultType = '维保';
			let that = this;
			const res = await this.$myRequest({
				url: '/api/v1/tms/addcomprehensiveresults',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});
				setTimeout(() => {
					this.cancelExcuse();
					this.cancelExcuse1();
				}, 2000);
				that.getExecute();
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'error'
				});
			}
		},
		excuteMakeManager(item) {
			this.formValue1 = item;
			this.$refs.popup1.open();
		},
		async confirmApprove(item) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/comprehensiveresultsupadete',
				data: item
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'success'
				});
				setTimeout(() => {
					this.cancelExcuse();
					this.cancelExcuse1();
				}, 2000);
				this.getExecute();
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'error'
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.executeResult {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	&_top {
		width: 100vw;
		flex: auto;
	}
	.popContent {
		width: 100vw;
		height: 100%;
		background-color: #fff;
		.main {
			padding-top: 50rpx;
			margin-bottom: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-direction: column;
			.buttonClass {
				width: 100%;
				flex: auto;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
}
</style>
